import { openStream } from '@/api/camera'
import { cameraUrl } from '@/shared'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useVideoStore = defineStore('video', () => {
  const selectedCameraUrl = ref('')

  function changeVideo(url: string) {
    cameraUrl.value = ''
    selectedCameraUrl.value = url
    if (url.includes('webrtc')) {
      cameraUrl.value = url
    } else {
      getWebRTCUrl()
    }
  }

  async function getWebRTCUrl() {
    if (!selectedCameraUrl.value) return
    const res = await openStream(selectedCameraUrl.value)
    cameraUrl.value = res.message
  }

  return {
    /**
     * 已选择的 rstp 地址
     */
    selectedCameraUrl,
    /**
     * 切换视频
     */
    changeVideo,
    /**
     * 获取 webrtc 拉流地址
     */
    getWebRTCUrl
  }
})
